<?php
require_once 'Admin/ActionEditController.php';
require_once 'Admin/ActionLoginController.php';
require_once 'Admin/ActionRegisterController.php';
require_once 'Admin/ActionPasswordController.php';
require_once 'Admin/ActionEditPhoneController.php';
require_once APPLICATION_PATH.'\misc\Paging.php';
require_once APPLICATION_PATH.'\misc\additional.php';
require_once APPLICATION_PATH.'\misc\exceptionHandler.php';
require_once APPLICATION_PATH.'\misc\sessionController.php';
class AdminController extends Zend_Controller_Action
{
    private $controller='Admin';
    private $classname='AdminController';
    protected  $_flashMessenger=null;
    public function init()
    {
        $sessionController=new sessionController();
        $this->view->sessioncontroller = $sessionController;
        
    }

    public function indexAction()
    {
       try{
            $sessionController=new sessionController();
            $paramName=array('Super','Admin','Keuangan','Kurir');
                    if($sessionController->checkSessionAdmin($paramName)){
                        //initialize table
                        $admin = new Application_Model_DbTable_Admin();
                        $role = new Application_Model_DbTable_Role();
                        $semuaDataAdmin = new Application_Model_DbTable_Admin();
                        
                        //get admin record
                     //   $rowAdmin = $admin->getAdmin($sessionController->id);
                        $this->view->id=$sessionController->getSessionId();
                        $this->view-> admin=$semuaDataAdmin->fetchRow('admin_id =' .
                                            $sessionController -> getSessionId());
                        $this->view->message = $this->_helper->_flashMessenger->getMessages();
                        $this->render();
                    }else{
                        $this->_helper->redirector('login');
                    }
            
        }catch(Zend_Exception $e){
           //initialize exceptionHandler
           $exceptionHandler = new exceptionHandler();
           $exceptionHandler->addLog($e, $this->classname);
    
    
    
    }
    }
    public function loginAction()
    {
      try{
        $this->_helper->layout->disableLayout();
        $this->view->errormessages="";
        //Initialize actionRegistrationBuyer
        $actionAdmin = new Application_Controllers_Admin_ActionLoginController();
        //Initialize Form
        $form = new Application_Form_Admin();
        //set Label for every instance
        $form->username->setLabel('Username');
        $form->password->setLabel('Password');
        $form->submit->setLabel('Submit');
        $form->reset->setLabel('Reset');
        //Initialize Form to Display
        $formDisplay = new Application_Form_Display();
        //Adding Elements to Form Display
        $formDisplay->addElements(array($form->username,
                                        $form->password,$form->submit,$form->reset ));
        $this->view->form = $formDisplay;
        
        if ($this->getRequest()->isPost()) {        
            $formData = $this->getRequest()->getPost();
                if ($formDisplay->isValid($formData)) {
                    if($actionAdmin->preLogin($formDisplay)){
                        $actionAdmin->onLogin($formDisplay);
                        $this->_helper->redirector('index');
                       }else{
                        $this->view->errormessages=$actionAdmin->wrongMessage;
                        $formDisplay->populate($formData);}
                }else{
                    $formDisplay->populate($formData);}
            }
        }catch(Zend_Exception $e){
           $exceptionHandler=new exceptionHandler();
           $exceptionHandler->addLog($e,$this->classname);
    }
    }
    public function addAction()
    {
        try{
        $sessionController=new sessionController();
        $paramName=array('Super');
        if($sessionController->checkSessionAdmin($paramName) ){
        // initialize form
        $form = new Application_Form_Admin();
        $formDisplay = new Application_Form_Display();
        $role = new Application_Model_DbTable_Role();
        
        $form->submit->setLabel("Add");      
        //form to be displayed
        $formDisplay->addElements(array($form->name,
                                  $form->email,$form->phone,$form->details,
                                  $form->role,$form->submit,$form->reset));
        $this->view->form = $formDisplay;
        
        if($this->getRequest()->isPost()){
            $postData = $this->getRequest()->getPost();
            //Validate input data
            if($formDisplay->isValid($postData)){
                $adminController = new Application_Controllers_Admin_ActionRegisterController();
                $adminController->onRegistration($formDisplay);                             
                //Redirect back to list admin
                $this->_helper->_flashMessenger->addMessage("Admin baru telah berhasil di Daftarkan");
                $this->_helper->redirector('list');
            }
            //Input is not valid
            else{
                $formDisplay->populate($postData);
                }
            }
            }else{
                $this->_helper->redirector('login');
            }
        }catch(Zend_Exception $e){
           $exceptionHandler=new exceptionHandler();
           $exceptionHandler->addLog($e,$this->classname);   
    }
    }
    public function editAction()
    {
        try{
        $role=new Application_Model_DbTable_Role();
        $id = $this->_getParam('id', 0);
        $sessionController=new sessionController();
        $paramName=array('Super','Admin','Kurir','Keuangan');
        $super=array('Super');
        if(($sessionController->checkSessionAdmin($paramName)
                && $sessionController->getSessionId()==$id)||
                ($sessionController->checkSessionAdmin($super))){
        
        $admin = new Application_Model_DbTable_Admin();
        $row = $admin->getAdmin($id);
        $form = new Application_Form_Admin();
        $form->submit->setLabel('Update');
        $formDisplay = new Application_Form_Display();
        $formDisplay->addElements(array($form->admin_id,$form->name,$form->email,
                                        $form->phone,$form->details,$form->submit,$form->reset));
        $this->view->form = $formDisplay;

        if($this->getRequest()->isPost()){
            $postData = $this->getRequest()->getPost();
            //Validate input data
            if($formDisplay->isValid($postData)){
                $admin = new Application_Model_DbTable_Admin();
                $adminController = new Application_Controllers_Admin_ActionEditController();
                $adminController->onEdit($formDisplay);
                $this->_helper->_flashMessenger->addMessage("Update telah berhasil Anda lakukan");
        
                //Redirect back to index
                $paramName=array('Super');
                if($sessionController->checkSessionAdmin($paramName)
                        && $sessionController->getSessionId() != $id){
                    $this->_helper->redirector('list');
                    
                    }else{
                 
                    $this->_helper->redirector('index');}
            }else{
                $formDisplay->populate($postData);
            }
        }else {           
            if ($id > 0) {                
                $this->view->role=$role->getRoleName($row['role_id']);
                $formDisplay->populate($admin->getAdmin($id));
            }
        }
        }else{
            $this->_helper->redirector('login');
        }
        }catch(Zend_Exception $e){
           $exceptionHandler=new exceptionHandler();
           $exceptionHandler->addLog($e,$this->classname); 
        }
    
    }

    public function detailsAction()
    {
       try{

            Zend_Session::start();
            $sessionController=new sessionController();
            $id = $this->_getParam('id', 0);
            $super=array('Super');
            $paramName=array('Super','Admin','Keuangan','Kurir');
        if(($sessionController->checkSessionAdmin($paramName)
                && $sessionController->getSessionId()==$id)||
                ($sessionController->checkSessionAdmin($super))){
                $admin = new Application_Model_DbTable_Admin();
                //get record by admin_id
                $row = $admin->getAdmin($id);
                $this->view->admin = $admin->getAdmin($id);
                $this->view->role = new Application_Model_DbTable_Role();
             }else{
                $this->_helper->redirector('login');
            }
        }catch(Zend_Exception $e){
           $exceptionHandler=new exceptionHandler();
           $exceptionHandler->addLog($e,$this->classname); 

        }

    }

    public function listAction()
    {
        try{

        $sessionController=new sessionController();
        $paramName=array('Super');

        $this->view->message = $this->_helper->_flashMessenger->getMessages();

        if($sessionController->checkSessionAdmin($paramName) ){ 
                $page=  $this->_getParam('page', 1);
                $order=  $this->_getParam('order', "admin_id");
                $method = $this->_getParam('method', "asc");
                $name = strtolower($this->_getparam('name',''));
                $admin = new Application_Model_DbTable_Admin();
                $where="";
                if($name!=""){
                    $where = " and c.name like '%".$name."%'";
                }
                $this->view->admins = $admin->getAll("c.role_id <>1 and c.deleted<>1".$where,$order,$method,$page, Paging::$number);
                $this->view->page=$page;
                $this->view->order=$order;
                $this->view->premethod=$method;
                if($method=='asc'){
                    $this->view->method='desc';
                }else{
                    $this->view->method='asc';
                }
                $paging = new Paging();
                $this->view->name=$name;
                $this->view->totalPages=  Paging::totalPages($admin->countRows("c.role_id <>1 and c.deleted<>1".$where));
                $this->view->back=  $paging->back($this->controller, 'list', $page, $order, $method,$name);
                $this->view->next=  $paging->next($this->controller, 'list',
                            $this->view->totalPages, $page, $order, $method,$name);
                $this->view->current = $paging->current($this->controller, 'list',
                        $this->view->totalPages, $page, $order, $method,$name);
                $this->view->role = new Application_Model_DbTable_Role();}else{
                $this->_helper->redirector('login');



            }

        }catch(Zend_Exception $e){
           $exceptionHandler=new exceptionHandler();
           $exceptionHandler->addLog($e,$this->classname); 
        }
    
    }

    public function logoutAction()
    {try{
            $sessionController=new sessionController();
            $paramName=array('Super','Admin','Keuangan','Kurir');
            if($sessionController->checkSessionAdmin($paramName)){
            
             $role = new Application_Model_DbTable_Role();
             $actionLog = new Application_Model_DbTable_Actionlog();
             $log = new Application_Model_DbTable_Log();
             $actionLog->addActionLog($sessionController->getSessionLogId(),
                                            '['.$_SERVER['REMOTE_ADDR']
                                            .'] Log Out');
            $log->updateLogOut($sessionController->getSessionLogId());
            $sessionController->logOut();
            $this->_helper->redirector('login');
        }else{
            $this->_helper->redirector('login');
        }
    }catch(Zend_Exception $e){
           $exceptionHandler=new exceptionHandler();
           $exceptionHandler->addLog($e,$this->classname);
    }
    }
    public function removeAction()
    {
     try{
        $admin=new Application_Model_DbTable_Admin();
        $role=new Application_Model_DbTable_Role();
        $sessionController=new sessionController();
        $paramName=array('Super');
        if($sessionController->checkSessionAdmin($paramName) ){ 
                $id = $this->_getParam('id', 0);                           
                $row=$admin->getAdmin($id);
                $admin->deleteAdmin($id);
                $actionLog = new Application_Model_DbTable_Actionlog();
                $actionLog->addActionLog($sessionController->getSessionLogId(),
                                            "[".$_SERVER['REMOTE_ADDR']
                                             ."][".$sessionController->getSessionUsername().
                                              "][Super Admin] Hapus User ".
                                             "Name : ".$row['name']
                                             ." Username :".$row['username']
                                             ." role:".$role->getRoleName($row['role_id']));

                $this->_helper->_flashMessenger->addMessage("Admin telah di Hapus");
                $this->_helper->redirector('list');
              }else{
                  $this->_helper->redirector('list');
              }
        }catch(Zend_Exception $e){
           $exceptionHandler=new exceptionHandler();
           $exceptionHandler->addLog($e,$this->classname);
    }
    }
    public function logviewerAction()
    {
        try{
        $sessionController=new sessionController();
        $paramName=array('Super');
        if($sessionController->checkSessionAdmin($paramName) ){ 
                    $log=new Application_Model_DbTable_Actionlog();
                    $this->view->log=$log->getLog();
              }else{
                    $this->_helper->redirector('list');
              }
        }catch(Zend_Exception $e){
           $exceptionHandler=new exceptionHandler();
           $exceptionHandler->addLog($e,$this->classname); 
    }
    }
    public function passwordAction()
    {
       try{
        $id = $this->_getParam('id', 0);
        $sessionController=new sessionController();
        $paramName=array('Super','Admin','Kurir','Keuangan');
        if($sessionController->checkSessionAdmin($paramName) ){ 
        $admin = new Application_Model_DbTable_Admin();
        $role = new Application_Model_DbTable_Role();

        $row = $admin->getAdmin($id);
        $form = new Application_Form_Admin();
        $form->password->setLabel('Password Lama');
        $form->submit->setLabel('Ubah Password');
        $formDisplay = new Application_Form_Display();
        $formDisplay->addElements(array($form->password,$form->newpassword,
                                  $form->repassword,$form->submit,$form->reset));
        $this->view->form = $formDisplay;

        if($this->getRequest()->isPost()){
            $postData = $this->getRequest()->getPost();
            //Validate input data
            if($formDisplay->isValid($postData)){
                $admin = new Application_Model_DbTable_Admin();
                $adminController = new Application_Controllers_Admin_ActionPasswordController();
                if($adminController->prePassword($sessionController->getSessionUsername(), $formDisplay)){
                    $adminController->onPassword($sessionController->getSessionId(), $formDisplay);
                    //Redirect back to index
                    $this->_helper->_flashMessenger->addMessage('Anda telah berhasil mengganti Password Anda!');
                    $this->_helper->redirector('index','admin');
                }else{
                    $this->view->errormessage=$adminController->wrongMessage;
                }              
            }else{
                $formDisplay->populate($postData);
            }
        }else {
            if ($id > 0) {
                $this->view->role=$role->getRoleName($row['role_id']);
                $formDisplay->populate($admin->getAdmin($id));
            }
        }
        }else{
            $this->_helper->redirector('login');
        }
        }catch(Zend_Exception $e){
           $exceptionHandler=new exceptionHandler();
           $exceptionHandler->addLog($e,$this->classname);  
           
        }
    
    }

    public function searchAction()
    {$this->_helper->layout->disableLayout();
        $this->_helper->layout->disableLayout();
        try{
        $this->view->role = new Application_Model_DbTable_Role();
        $admins = new Application_Model_DbTable_Admin();
        $sessionController=new sessionController();
        $paramName=array('Super','Admin','Kurir','Keuangan');
        if($sessionController->checkSessionAdmin($paramName) ){ 
            
            $this->type=$this->_getParam('type', 0);
            $this->id=$this->_getParam('id', 0);
            $this->adminId=$this->_getParam('adminId', 0);
            $this->page=$this->_getParam('page', 0);
            $this->view->type = $this->type;
            $this->view->page = $this->page;
             if( $this->type=="display"){
                    if($this->page=="list"){
                        if(isset($_POST['name'])) {
                       // $page=  $this->_getParam('page', 1);
                            $order=  $this->_getParam('order', "name");
                            $method = $this->_getParam('method', "asc");
                            if(isset($_POST['name'])){
                                $name = strtolower($this->_getparam('name',''));
                            }else{
                                $name='';
                            }
                            $admin = new Application_Model_DbTable_Admin();
                            $where="";
                            if($name!=""){
                                $where = " and c.name like '%".strtolower($name)."%'";
                            }
                            $this->view->admins =
                                    $admin->getAll("c.role_id <>1 and c.deleted<>1".$where,$order,
                                                    $method,1, Paging::$number);
                            $this->view->order=$order;
                            $this->view->premethod=$method;
                            if($method=='asc'){
                                $this->view->method='desc';
                            }else{
                                $this->view->method='asc';
                            }
                            $paging = new Paging();
                            $this->view->name=$name;
                            $this->view->totalPages=  Paging::totalPages($admin->countRows(" c.role_id<>1".$where));
                            $this->view->back=  $paging->back($this->controller, 'list', 1, $order, $method,$name);
                            $this->view->next=  $paging->next($this->controller, 'list',
                            $this->view->totalPages, 1, $order, $method,$name);
                            $this->view->current = $paging->current($this->controller, 'list',
                            $this->view->totalPages, 1, $order, $method,$name);
                            $this->view->role = new Application_Model_DbTable_Role();
                         }
                            
                        
            }else if($this->page=='peran' && $this->type!="change"){
                       if(isset($_POST['name'])) {

                            $order=  $this->_getParam('order', "name");
                            $method = $this->_getParam('method', "asc");
                            if(isset($_POST['name'])){
                                $name = strtolower($this->_getparam('name',''));
                            }else{
                                $name='';
                            }
                            $admin = new Application_Model_DbTable_Admin();
                            $where="";
                            if($name!=""){
                                $where = " and c.name like '%".strtolower($name)."%'";
                            }
                            $this->view->admins =
                                    $admin->getAll("c.role_id <>1 and c.deleted<>1".$where,
                                            $order,$method,1, Paging::$number);
                       
                            $this->view->order=$order;
                            $this->view->premethod=$method;
                            if($method=='asc'){
                                $this->view->method='desc';
                             }else{
                                $this->view->method='asc';
                            }
                            $paging = new Paging();
                            $this->view->name=$name;
                            $this->view->totalPages=
                                    Paging::totalPages($admin->countRows
                                            (" c.role_id<>1".$where));
                            $this->view->back=
                                    $paging->back($this->controller, 'changerole', 1,
                                                  $order, $method,$name);
                            $this->view->next=  $paging->next($this->controller, 'changerole',
                                                         $this->view->totalPages,
                                                         1, $order, $method,$name);
                            $this->view->current = $paging->current($this->controller, 'changerole',
                                                    $this->view->totalPages, 1,
                                                    $order, $method,$name);
                            $this->view->role = new Application_Model_DbTable_Role();
                            $form = new Application_Form_Admin();
                            $formDisplay = new Application_Form_Display();
                            $formDisplay->addElements(array($form->role));
                            //First, retrive all existing roles from DB except superadmin
                            $this->view->form=$formDisplay;
                         }
               }
                        
            }else if($this->type=="change"){
                        
                        $role=new Application_Model_DbTable_Role();
                        $namayangdiganti=new Application_Model_DbTable_Admin();
                        $row = $namayangdiganti->getAdmin($this->adminId);
                        $data = array("role_id"=>$this->id);
                        $where =" admin_id=". (int) $this->adminId;
                        $admins->update($data, $where);
                        $this->view->row = $admins->fetchAll();
                        //insert action into log
                        $actionLog = new Application_Model_DbTable_Actionlog();
                        $actionLog->addActionLog($sessionController->getSessionLogId(),
                                            "[".$_SERVER['REMOTE_ADDR'].
                                            "] Ubah Peran ".
                                            "User_ID :".$this->adminId."
                                             menjadi Peran : ".$this->id."  ");
                        
                        $this->_helper->_flashMessenger->addMessage("Peran Admin ".$row['name']. 
                                                        " telah di ganti dari ".$role->getRoleName($row['role_id']).
                                                        " menjadi ".$role->getRoleName($this->id));
                        $this->_helper->redirector('changerole');
               }else if( $this->type=="date"
                            && isset($this->type) && $this->type=="date"){
                        $log=new Application_Model_DbTable_Actionlog();
                        $this->view->log=$log->getDateLog($_POST['date']);
                    }
     }else{
        $this->_helper->redirector('login');}
 }catch(Zend_Exception $e){
    $exceptionHandler=new exceptionHandler();
     $exceptionHandler->addLog($e,$this->classname); }

 }


    public function summaryAction()
    {
        try{
        $sessionController=new sessionController();
        $paramName=array('Super');
        if($sessionController->checkSessionAdmin($paramName) ){
                $id = $this->_getParam('id', 0);
                $admin = new Application_Model_DbTable_Admin();
                //get record by admin_id
                $this->view->admin = $admin;
               
             }else{
                $this->_helper->redirector('login');
            }
        }catch(Zend_Exception $e){
           $exceptionHandler=new exceptionHandler();
           $exceptionHandler->addLog($e,$this->classname); 
    }
    }
    public function changeroleAction()
    {
        $this->view->message=$this->_helper->_flashMessenger->getMessages();
    try{
        $sessionController=new sessionController();
        $paramName=array('Super');
        $form=new Application_Form_Admin();
        $formDisplay = new Application_Form_Display();
        if($sessionController->checkSessionAdmin($paramName) ){ 
                $page=  $this->_getParam('page', 1);
                $order=  $this->_getParam('order', "admin_id");
                $method = $this->_getParam('method', "asc");
                $name = strtolower($this->_getparam('name',''));
                $admin = new Application_Model_DbTable_Admin();
                $where="";
                if($name!=""){
                    $where = " and c.name like '%".$name."%'";
                }
                $this->view->admins = $admin->getAll("c.role_id <>1 and c.deleted <>1 ".$where,$order,$method,$page, Paging::$number);
                $this->view->page=$page;
                $this->view->order=$order;
                $this->view->premethod=$method;
                if($method=='asc'){
                    $this->view->method='desc';
                }else{
                    $this->view->method='asc';
                }
                $paging = new Paging();
                $this->view->name=$name;
                $this->view->totalPages=  Paging::totalPages($admin->countRows("c.role_id <>1 and c.deleted <>1 ".$where));
                $this->view->back=  $paging->back($this->controller, 'changerole', $page, $order, $method,$name);
                $this->view->next=  $paging->next($this->controller, 'changerole',
                            $this->view->totalPages, $page, $order, $method,$name);
                $this->view->current = $paging->current($this->controller, 'changerole',
                        $this->view->totalPages, $page, $order, $method,$name);
                $this->view->role = new Application_Model_DbTable_Role();
                $formDisplay->addElements(array($form->role));
                $this->view->form=$formDisplay;
               
                }else{
                
                $this->_helper->redirector('login');
            }
        }catch(Zend_Exception $e){
           $exceptionHandler=new exceptionHandler();
           $exceptionHandler->addLog($e,$this->classname);
        }

     }



     public function editphoneAction()
    {
       try{

        $sessionController=new sessionController();
        $paramName=array('Super');
        if($sessionController->checkSessionAdmin($paramName) ){
        $id=$sessionController->getSessionId();
        $admin = new Application_Model_DbTable_Admin();
        $role = new Application_Model_DbTable_Role();
        $row = $admin->getAdmin($id);
        $form = new Application_Form_Admin();
        $form->phone->setLabel('No Telephone Lama');
        $form->submit->setLabel('Ubah No Telephone');
        $formDisplay = new Application_Form_Display();
        $formDisplay->addElements(array($form->phone,$form->newphone,
                                  $form->submit));
        $this->view->form = $formDisplay;

        if($this->getRequest()->isPost()){
            $postData = $this->getRequest()->getPost();
            //Validate input data
            if($formDisplay->isValid($postData)){
                $admin = new Application_Model_DbTable_Admin();
                $adminController = new Application_Controllers_Admin_ActionEditPhoneController();
                if($adminController->prePhoneNumber($sessionController->getSessionUsername(), $formDisplay)){
                    $adminController->onPhoneNumber($sessionController->getSessionId(), $formDisplay);
                    $actionLog = new Application_Model_DbTable_Actionlog();
                    $actionLog->addActionLog($sessionController->getSessionLogId(),
                                            "[".$_SERVER['REMOTE_ADDR'].
                                            "] Ubah Nomor Telepon ".
                                            "Username :".$row['username']." ");
                    //Redirect back to index
                    $this->_helper->_flashMessenger->addMessage('Anda telah berhasil mengganti Nomor Telephon Anda');
                    $this->_helper->redirector('index','admin');



                }else{
                    $this->view->errormessage=$adminController->wrongMessage;

                }


            }else{
                $formDisplay->populate($postData);
            }
        }else {
            if ($id > 0) {
                $this->view->role=$role->getRoleName($row['role_id']);
                $formDisplay->populate($admin->getAdmin($id));
            }
        }
        }else{
            $this->_helper->redirector('login');
        }
        }catch(Zend_Exception $e){
           $exceptionHandler=new exceptionHandler();
           $exceptionHandler->addLog($e,$this->classname);
        }

    }

 
   

}


















